package org.NooLab.glue.filter;

import java.util.Vector;

import org.NooLab.glue.MessageBoard;
import org.NooLab.glue.components.ClientMatcherIntf;
import org.NooLab.glue.components.GlobalReceipts;
import org.NooLab.glue.components.MessageBoardFactory;
import org.NooLab.glue.components.MessageBoardFactoryProperties;
import org.NooLab.glue.components.MessageBoardTrafficMonitorIntf;
import org.NooLab.glue.components.SenderWrap;
import org.NooLab.glue.msgframe.MetaRegistration;
import org.NooLab.glue.msgframe.XMessage;
import org.NooLab.glue.msgframe.XRegistration;
import org.NooLab.glue.net.BoardAddresses;
import org.NooLab.glue.net.ComBoxIntf;
import org.NooLab.glue.storage.MsgBoardDataStorage;
import org.NooLab.glue.subscription.FilterPackageIntf;
import org.NooLab.glue.subscription.FilterPackages;
import org.NooLab.glue.transaction.Transactions;
import org.NooLab.utilities.files.DFutils;
import org.NooLab.utilities.logging.PrintLog;
import org.NooLab.utilities.strings.StringsUtil;

 

public class MessageInterpreter {

	MessageBoard messageBoard;
	MessageBoardFactory mbf=null;
	
	MetaRegistration metaRegistration;
	
	ComBoxIntf comBox;
	
	
	GlobalReceipts globalReceipts;
 	
	Transactions transactions;
	MsgBoardDataStorage msgBoardStorage;
	
	MessageBoardTrafficMonitorIntf  msgMonitor;
	
	FilterPackageIntf filterPackage;
	
	BoardAddresses boardAddresses ; 
	Vector<XRegistration> registeredInstances ;
	
	ClientMatcherIntf clientMatcher;
	
	boolean asyncProtocol;
	
	protected String originatorID="" ;
	int protocolID= -1;
	
	int _old_value = MessageBoardFactoryProperties.__OLD ;
	int _veryold_value = MessageBoardFactoryProperties.__VERY_OLD ;
	
	
	PrintLog out;
	DFutils fileutil = new DFutils(); 
	StringsUtil strgutil = new StringsUtil (); 
	
	
	
	public MessageInterpreter(  MessageBoard messageboard, 
			 					FilterPackages filters, 
			 					PrintLog outprn ){
		out = outprn;
		this.messageBoard = messageboard;
		
		mbf = messageBoard.getMsgBoardShares().getMsgBoardFactory() ;
		
		init() ;
	}
	
	public MessageInterpreter(  MessageBoardFactory mbf, 
								FilterPackages filters, 
								PrintLog outprn ){
		this.mbf = mbf;
		messageBoard = mbf.getMessageBoard() ;
		
		init() ;
	}
	
	public void init(){
		int pid=0;
		
		boardAddresses  = mbf.getMsgBoardShare().getBoardAddresses();
		
		metaRegistration = mbf.getMetaRegistration();
		
		// comBox = messageBoard.getComBox() ;
		comBox = mbf.getComBoxFactory().getComBox(); 
		
		msgBoardStorage = mbf.getMessageBoardStorage(); //
		
		if (messageBoard!=null){
			globalReceipts = messageBoard.getGlobalReceipts();
		
			transactions = messageBoard.getTransactions() ;
		
			clientMatcher = messageBoard.getClientMatcher() ;
			// only if the main instance is including the MessageBoard, we need it
			// and only then the clientMatcher is != null
			if (clientMatcher!=null){
				clientMatcher.setMessageBoard(messageBoard) ;
			}
		}
		 
		registeredInstances =  mbf.getMetaRegistration().getRegisteredInstances()  ;
		
		protocolID =  mbf.getComBoxFactory().getProtocolID() ;
		
		// pid = messageBoard.getProtocolID()
		pid = mbf.getFactoryProperties().getProtocolId();
		asyncProtocol = (pid <= MessageBoardFactoryProperties.protocolFTP ) ;
		
 
	}
	
	

	 
	protected int wrappedSending( XMessage xmsg, XRegistration xreg, String xmlOutStr , 
								  String restletPath, String httpMethod, int port){
		int resultVal = -1;
		
		 
		String xmlRestPrefix="";
		boolean hb;
		SenderWrap sender ;
		
		
		// .......................................
		
		if (xmlOutStr.length()<=1){
			return resultVal;
		}
		sender = new SenderWrap(messageBoard);

		if (protocolID >= MessageBoardFactoryProperties.protocolTCP) {
			
			if ((port<=0) && (xreg!=null)){
				port = xreg.getClientListeningPort();
			}
			
		} else {
			// asynchronous protocols, requiring handshakes, confirmations, in-session replies ...
			port = messageBoard.getMsgPortforSources();
			
			// meta prolog using tags comprising meta data about restlet path ...
			// this will be removed by the restlet framework before sending the string !
			// it is just for internal control 
			
			if (protocolID <= MessageBoardFactoryProperties.protocolHREST){
				// xmlRestPrefix = xmsg.createRestletRoutingPrefix("", "/data", "post");
				if (restletPath.length()==0){
					// restletPath = "/data";
				}
				if (httpMethod.length()==0){
					// httpMethod = "post";
				}
						
				xmlRestPrefix = xmsg.createRestletRoutingPrefix("", restletPath, httpMethod);
				xmlOutStr = xmlRestPrefix + xmlOutStr;
			}
			if (protocolID <= MessageBoardFactoryProperties.protocolHREST){ 
				// nothing will be added if the originatorID == "" in case of non-http-rest !!
				xmlOutStr = xmsg.insertRawToXml( originatorID, "originator","id", xmlOutStr, "</messageboard>",1);
			}
		}

		hb = false;
		
		if (protocolID >= MessageBoardFactoryProperties.protocolTCP) {
			if (port > 1000) {
				hb = true;
			}
		} else {
			if (xreg!=null){
				port = xreg.getActiveport();
			}
			hb = true;
		}
		
		
		if (hb) {

			int r=sender.sendingMessage(xmlOutStr, port);
			resultVal = 0;
			sender=null;
		}

		return resultVal;
	}
	 
	@SuppressWarnings("static-access")
	protected void delay(int millis){
		try {
				Thread.currentThread().sleep(millis);
		} catch (Exception e) {}
	}

	
	protected String stripTransactionFromXmlMsg( XMessage xmsg, String rawXmlMsg, String embeddingElement ){
		
		String resultXml="";
		
		try{
			
			// xml = xmsg.removeDataSection(rawXmlMsg, embeddingElement);
		}catch(Exception e){
			
		}
		
		
		return resultXml;
	}
	
	
	
	public Transactions getTransactions() {
		return transactions;
	}

	public void setTransactions(Transactions transactions) {
		this.transactions = transactions;
	}
}
